<template>
  <div :class="className" :style="{width: width,height:height}"></div>
</template>

<script>
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '400px'
    },
    height: {
      type: String,
      default: '400px'
    },
    option: Object
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    option: {
      handler(newValue) {
        this.chart.setOption(newValue, true)
      },
      deep: true
    },
  },
  mounted() {
    this.initChart()
  },
  methods: {
    initChart() {
      if (!this.chart) {
        this.chart = this.$echarts.init(this.$el)
      }
      this.chart.setOption(this.option, true)
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return false
    }
    this.chart.dispose()
    this.chart = null
  }
}
</script>

<style>
</style>
